/*
 * Copyright 2018-2019 KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain menu_text_color copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gzwanchuang.xhgj.ui.page.login;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.gzwanchuang.architecture.utils.FileUtils;
import com.gzwanchuang.architecture.utils.GsonUtils;
import com.gzwanchuang.architecture.utils.ImageUtils;
import com.gzwanchuang.architecture.utils.Utils;
import com.gzwanchuang.architecture.utils.ValidateUtils;
import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bridge.request.LoginRequestViewModel;
import com.gzwanchuang.xhgj.bridge.state.login.MENetworkFourViewModel;
import com.gzwanchuang.xhgj.databinding.FragmentMeNetworkFourBinding;
import com.gzwanchuang.xhgj.help.ImageHelper;
import com.gzwanchuang.xhgj.help.PermissionHelper;
import com.gzwanchuang.xhgj.ui.base.BaseFragment;
import com.gzwanchuang.xhgj.ui.dialog.RecycleViewDialog;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import kale.ui.view.dialog.EasyDialog;

import static android.app.Activity.RESULT_OK;


/**
 * Create by KunMinX at 19/10/29
 */
public class MENetworkFourFragment extends BaseFragment {

    private FragmentMeNetworkFourBinding mBinding;
    private MENetworkFourViewModel mViewModel;
    private LoginRequestViewModel mLoginRequestViewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(MENetworkFourViewModel.class);
        mLoginRequestViewModel = ViewModelProviders.of(this).get(LoginRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me_network_four, container, false);
        mBinding = FragmentMeNetworkFourBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mLoginRequestViewModel.getErrorString().observe(this, s -> {
         hideProgress();
            showShortToast(s);
        });  // 错误返回
        mLoginRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            nav().navigate(R.id.action_MENetworkFourFragment_to_MENetworkFiveFragment);
        });
        mLoginRequestViewModel.getCodeString().observe(this, s -> {
            showShortToast(s);
            hideProgress();
            mViewModel.startCode.set(true);
        });
        mLoginRequestViewModel.getCompathone().observe(this, s -> {  // 上传第一步返回
            mViewModel.UploadPath = s;
            mLoginRequestViewModel.upLoadImage(mViewModel.compressPathTwo.get(), mLoginRequestViewModel.getCompathTwo());
        });
        mLoginRequestViewModel.getCompathTwo().observe(this, s -> {  // 上传第二步返回
            mViewModel.UploadPathTwo = s;
            mLoginRequestViewModel.upLoadImage(mViewModel.compressPathThree.get(), mLoginRequestViewModel.getCompathThree());
        });
        mLoginRequestViewModel.getCompathThree().observe(this, s -> {  // 上传第三步返回
            mViewModel.UploadPathThree = s;
            mLoginRequestViewModel.audit(mViewModel.UploadPath, mViewModel.UploadPathTwo, mViewModel.UploadPathThree
                    , mViewModel.name.get(), mViewModel.bankCardCode.get(), mViewModel.phone.get(), mViewModel.code.get());
        });
        mLoginRequestViewModel.getBanks().observe(this, banks -> {
            mViewModel.banks.addAll(banks);
        });

        mSharedViewModel.bankCardString.observe(this, s -> {
            if (mViewModel.selectImageType == 0) {
                mViewModel.compressPath.set(s);
            } else if (mViewModel.selectImageType == 1) {
                mViewModel.compressPathTwo.set(s);
            }
        });

        mLoginRequestViewModel.get_support("1", "2");  // 类型:1-结算卡;2-信用卡   // VIP支持：1-支持;2-所有
    }


    public class ClickProxy {
        public void submit() {
            String name = mViewModel.name.get();
            if (TextUtils.isEmpty(name)) {
                showShortToast("请输入姓名");
                return;
            }
            String bankCardCode = mViewModel.bankCardCode.get();
            if (TextUtils.isEmpty(bankCardCode)) {
                showShortToast("请输入银行卡号码");
                return;
            }
            String phone = mViewModel.phone.get();
            if (TextUtils.isEmpty(phone)) {
                showShortToast("请输入银行预留手机号");
                return;
            }
            String code = mViewModel.code.get();
            if (TextUtils.isEmpty(code)) {
                showShortToast("请输入手机验证码");
                return;
            }
            showProgress();
            // 开始上传
            mLoginRequestViewModel.upLoadImage(mViewModel.compressPath.get(), mLoginRequestViewModel.getCompathone());
        }

        public void back() {
            mSharedViewModel.isShowLoginOut.setValue(true);
        }

        public void checkBank() {
            if (ValidateUtils.isValidate(mViewModel.banks)) {
                RecycleViewDialog.Builder builder = new RecycleViewDialog.Builder(mActivity);
                builder.setData(GsonUtils.serializedToJson(mViewModel.banks)).setTitle("绑定结算卡支持银行");
                EasyDialog build = builder.build();
                build.show(getChildFragmentManager());
            } else {
                showShortToast("暂无支持银行");
            }
        }

        public void getCode() {
            String bankCardCode = mViewModel.bankCardCode.get();
            if (TextUtils.isEmpty(bankCardCode)) {
                showShortToast("请输入银行卡号码");
                return;
            }
            String mPhone = mViewModel.phone.get();
            if (TextUtils.isEmpty(mPhone)) {
                showShortToast("请输入手机号码");
                return;
            }
            if (mPhone.length() != 11) {
                showShortToast("请输入11位手机号码");
                return;
            }
            if (TextUtils.isEmpty(mPhone)) {
                showShortToast("请输入手机号码");
                return;
            }
            showProgress();
            mLoginRequestViewModel.getSendcode(mPhone, bankCardCode);
        }

        public void selectPicture(int type) {
            mViewModel.selectImageType = type;
            PermissionHelper.getInstance().getPermission(mActivity, new PermissionHelper.PermissionCallBack() {
                @Override
                public void onSuccess() {
                    ImageHelper.showSelectChoose(MENetworkFourFragment.this);
                }

                @Override
                public void onFail() {
                    showShortToast("请先允许权限再使用此功能");
                }
            }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }


        public void selectBankCard(int type) {
            mViewModel.selectImageType = type;
            Bundle bundle = new Bundle();
            bundle.putInt("type",0);
            nav().navigate(R.id.action_changeJieSuanKaFragment_to_takeCardPhotoFragment,bundle);
        }
    }


    @Override
    public void onStop() {
        if (Utils.isAppForeground() || !mSharedViewModel.isLoading.getValue()) {
            mViewModel.startCode.set(false);
        }
        super.onStop();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    }
}
